package com.castle.fortress.admin.flowable.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.flowable.entity.FlowableCommentExtendEntity;
import com.castle.fortress.admin.flowable.dto.FlowableCommentExtendDto;
import com.castle.fortress.admin.flowable.mapper.FlowableCommentExtendMapper;
import com.castle.fortress.admin.flowable.service.FlowableCommentExtendService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 流程评论扩展表 服务实现类
 *
 * @author castle
 * @since 2023-12-01
 */
@Service
public class FlowableCommentExtendServiceImpl extends ServiceImpl<FlowableCommentExtendMapper, FlowableCommentExtendEntity> implements FlowableCommentExtendService {
    /**
    * 获取查询条件
    * @param flowableCommentExtendDto
    * @return
    */
    public QueryWrapper<FlowableCommentExtendEntity> getWrapper(FlowableCommentExtendDto flowableCommentExtendDto){
        QueryWrapper<FlowableCommentExtendEntity> wrapper= new QueryWrapper();
        if(flowableCommentExtendDto != null){
            FlowableCommentExtendEntity flowableCommentExtendEntity = ConvertUtil.transformObj(flowableCommentExtendDto,FlowableCommentExtendEntity.class);
            wrapper.like(flowableCommentExtendEntity.getId() != null,"id",flowableCommentExtendEntity.getId());
            wrapper.like(flowableCommentExtendEntity.getTid() != null,"tid",flowableCommentExtendEntity.getTid());
            wrapper.like(flowableCommentExtendEntity.getDid() != null,"did",flowableCommentExtendEntity.getDid());
            wrapper.like(StrUtil.isNotEmpty(flowableCommentExtendEntity.getTaskId()),"task_id",flowableCommentExtendEntity.getTaskId());
            wrapper.like(StrUtil.isNotEmpty(flowableCommentExtendEntity.getPid()),"pid",flowableCommentExtendEntity.getPid());
            wrapper.like(StrUtil.isNotEmpty(flowableCommentExtendEntity.getActKey()),"act_key",flowableCommentExtendEntity.getActKey());
            wrapper.like(StrUtil.isNotEmpty(flowableCommentExtendEntity.getCommId()),"comm_id",flowableCommentExtendEntity.getCommId());
            wrapper.like(StrUtil.isNotEmpty(flowableCommentExtendEntity.getImages()),"images",flowableCommentExtendEntity.getImages());
            wrapper.like(StrUtil.isNotEmpty(flowableCommentExtendEntity.getFiles()),"files",flowableCommentExtendEntity.getFiles());
        }
        return wrapper;
    }


    @Override
    public IPage<FlowableCommentExtendDto> pageFlowableCommentExtend(Page<FlowableCommentExtendDto> page, FlowableCommentExtendDto flowableCommentExtendDto) {
        QueryWrapper<FlowableCommentExtendEntity> wrapper = getWrapper(flowableCommentExtendDto);
        Page<FlowableCommentExtendEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<FlowableCommentExtendEntity> flowableCommentExtendPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<FlowableCommentExtendDto> pageDto = new Page(flowableCommentExtendPage.getCurrent(), flowableCommentExtendPage.getSize(),flowableCommentExtendPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(flowableCommentExtendPage.getRecords(),FlowableCommentExtendDto.class));
        return pageDto;
    }


    @Override
    public List<FlowableCommentExtendDto> listFlowableCommentExtend(FlowableCommentExtendDto flowableCommentExtendDto){
        QueryWrapper<FlowableCommentExtendEntity> wrapper = getWrapper(flowableCommentExtendDto);
        List<FlowableCommentExtendEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,FlowableCommentExtendDto.class);
    }

    @Override
    public Map<String, FlowableCommentExtendEntity> selToMap(String processId) {
        QueryWrapper<FlowableCommentExtendEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",processId);
        List<FlowableCommentExtendEntity> list = baseMapper.selectList(wrapper);
        Map<String, FlowableCommentExtendEntity> map = new HashMap<>();
        for(FlowableCommentExtendEntity entity:list){
            map.put(entity.getCommId(),entity);
        }
        return map;
    }
}

